package com.dr.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dr.common.Const;
import com.dr.pojo.Constantitem;
import com.dr.pojo.Dept;
import com.dr.pojo.User;
import com.dr.pojo.Registlevel;
import com.dr.service.DeptService;
import com.dr.service.UserService;
import com.dr.service.constantitemService;
import com.dr.service.registlevelService;
@Controller
//声明是可接收的
//@RequestMapping("/um") //避免重复
@RequestMapping("/um")
public class UserController {
	@Autowired
	private UserService userservice;//Spring 注入ServiceImpl实例
	
	@Autowired
	private DeptService deptservice;
	
	@Autowired
	private registlevelService levelservice;
	
	@Autowired
	private constantitemService docTitleIDservice;
	/*
	 * 显示用户列表页面，加载所有用户信息
	*/
	@RequestMapping( "/userList")
	public ModelAndView userList(User user) {
		List<User> userList=userservice.findUserList();//		取得用户列表数据
		ModelAndView mav = new ModelAndView();//构建ModelAndView实例
		mav.addObject("userList", userList);
		//加载到用户列表页面中，相当于执行request.setAtrribute("userList",userList);
		mav.setViewName("userManage/userList");
//		设定跳转指定页面
		return mav;
 		}
	//根据页面输入的内容，查询用户
	@RequestMapping("/userListByName")
	public ModelAndView userListByName(String txtQuery) {
		List<User> userList=userservice.findUserByName(txtQuery);
//		取得用户列表数据
		ModelAndView mav = new ModelAndView();
		mav.addObject("userList", userList);
		mav.addObject("txtQuery", txtQuery);
//		加载到用户列表页面中，相当于执行request.setAtrribute("userList",userList);
		mav.setViewName("userManage/userList.jsp");
//		设定跳转指定页面
		return mav;
 		}
	//打开新增页面
	@RequestMapping("/toAdd")
	public ModelAndView toAdd() {
		List<Dept> deptList= deptservice.findDeptList();
		List<Registlevel> levelList= levelservice.findRegistlevelList();
		List<Constantitem> constList= docTitleIDservice.findConstantitemList();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/userAdd");
	    mav.addObject("deptList", deptList);
	    mav.addObject("levelList", levelList);
	    mav.addObject("constList", constList);
		return mav;
	}
	//打开修改页面
	@RequestMapping("/toUpdate")
	public ModelAndView toUpdate(Integer id) {
		List<Dept> deptList= deptservice.findDeptList();
		List<Registlevel> levelList= levelservice.findRegistlevelList();
		List<Constantitem> constList= docTitleIDservice.findConstantitemList();
		User user = userservice.findUserById(id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/userUpdate");
		mav.addObject("deptList", deptList);
		mav.addObject("levelList", levelList);
		mav.addObject("constList", constList);
		mav.addObject("user", user);
		return mav;
	}
	//打开修改密码页面
		@RequestMapping("/toPassword")
		public ModelAndView toPassword(String userName) {
			
//			User user = userservice.findUserByUserName(userName);
			ModelAndView mav = new ModelAndView();
			mav.setViewName("userManage/pwdUpdate");
			mav.addObject("userName", userName);	
			return mav;
		}
		
		//提交新密码
		@ResponseBody
		@RequestMapping("/doPassword")
		public Map<String, Object> doPassword(User user) {
			Map<String, Object> map =new HashMap<>();
			int result= userservice.updatePassword(user);
			if(result>0) {
				map.put("status", Const.STATUS_SUCESS);
			}else {
				map.put("status", Const.STATUS_FAILED);
			}
			 		 return map;
		}
//	新增用户
	@ResponseBody
	@RequestMapping("/doAdd")
	public Map<String,Object> doAdd(User user) {
		String userName =user.getUserName();
		User addUser =  userservice.findUserByUserName(userName);
		

		//		判断待新增的账户名是否存在

		Map<String,Object> map = new HashMap<>();
		if(addUser != null) {
//			如果存在提示账户存在，不执行insert,返回到新增页面
//	ajax已有		ModelAndView mav = toAdd();
//			mav.addObject("attrInfoUserAdd","账户已存在!");   //硬编码
			map.put("attrInfoUserAdd",Const.INF_USER_NAME_EXIST);
			map.put("status", Const.STATUS_FAILED);
		}else {
//			执行insert并返回userList
			int result = userservice.insertUser(user);
			if(result>0) {
				map.put("status", Const.STATUS_SUCESS);
			}else {
				map.put("status", Const.STATUS_FAILED);
			}
			}
//		mav.setViewName("userManage/userAdd");
		return map;
		//这里实际就是userList方法
	}
	
//	修改用户
	@ResponseBody
	@RequestMapping("/doUpdate")
	public Map<String, Object> doUpadte(User user) {
		Map<String, Object> map =new HashMap<>();
		int result = userservice.updateUser(user);
		if(result>0) {
			map.put("status", Const.STATUS_SUCESS);
		}else {
			map.put("status", Const.STATUS_FAILED);
		}
		return map;
		}
			
//	删除修改用户
	@ResponseBody
	@RequestMapping("/doDelete")
	public Map<String, Object> doDelete(Integer... id) {
		Map<String, Object> map =new HashMap<>();
		int result= userservice.deleteUser(id);
		if(result>0) {
			map.put("status", Const.STATUS_SUCESS);
		}else {
			map.put("status", Const.STATUS_FAILED);
		}
		 		 return map;
	
		
	}
}
